package com.nuatar.meibi;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Window;
import android.widget.Toast;

public class FirstActivity extends Activity {
	AlertDialog ald;
	boolean IsEnd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(getApplicationContext(),
				MsgAlarmReceiver.class);
		int requestCode = 0;
		PendingIntent pendIntent = PendingIntent.getBroadcast(
				getApplicationContext(), requestCode, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		alarmMgr.cancel(pendIntent);
		int interval = 60 * 1000;
		alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000,
				interval, pendIntent);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int scrw = dm.heightPixels;
		int scrh = dm.widthPixels;
		if(scrw<scrh){
			int d=scrw;
			scrw=scrh;
			scrh=d;
		}
		MeiBi mks=(MeiBi) this.getApplication();
		mks.scrw=scrw;
		mks.scrh=scrh;
		CrashHandler crashHandler = CrashHandler.getInstance();  
        crashHandler.init(this); 
		boolean sdCardExist = Environment.getExternalStorageState()
				.equals(android.os.Environment.MEDIA_MOUNTED);
		if(!sdCardExist){
			Toast.makeText(getApplicationContext(), "未检测到SD卡，退出！", Toast.LENGTH_LONG).show();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			System.exit(0);
		}
		ald = new AlertDialog.Builder(FirstActivity.this)
		.setMessage("当前无网络或当前网络不是无线网络，是否设置？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialoginterface, int i) {
				// 按钮事件
				startActivity(new Intent(Settings.ACTION_SETTINGS));
               
                	System.exit(0);
               
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialoginterface, int i) {
				// 按钮事件
				
				if(IsEnd){
                	System.exit(0);
                }else{
                	Jump();
                }
				
			}
		}).setOnCancelListener(new OnCancelListener(){

			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO 自动生成的方法存根
				if(IsEnd){
                	System.exit(0);
                }else{
                	Jump();
                }
			}})
		.create();
		ald.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		ConnectivityManager manager = (ConnectivityManager) getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ninfo = manager.getActiveNetworkInfo();
		if(ninfo==null||!ninfo.isAvailable()){
			IsEnd=true;
			ald.show();
			
		}else{
			IsEnd=false;
			if(ninfo.getType()!=ConnectivityManager.TYPE_WIFI){
				ald.show();
			}
			
		}
		if(!IsEnd&&ninfo.getType()==ConnectivityManager.TYPE_WIFI){
			Jump();
		}
	}
	public void Jump(){
		Intent intent = new Intent();
		intent.setClass(FirstActivity.this, MainActivity.class);
		//intent.setClass(FirstActivity.this, ShareActivity.class);
		startActivity(intent);
		finish();
	}
	
}
